<?php

namespace App\Http\Controllers\NonPublicVersion;

use App\Http\Controllers\ErrorCode;
use App\Http\Controllers\SuccessCode;
use App\Http\Requests\GameConfigRequest;
use App\Model\Channel;
use App\Model\GamePackage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\GameConfig as Model;
use App\Http\Controllers\Common;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
use Cache;


class GameConfig extends Controller
{
    use Common;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $name = $request->input('name' ,'');
        $key = $request->input('key' ,'');
        $page = $request->input('page' ,1);
        $pageSize = $request->get('paginate',config('web.page.num'));
        $query = [];
        $queryParams = [
            'name' => $name,
            'key' => $key,
        ];

        $where = [];
        if ($name){
            $where[] = ['app_name', 'like', "%{$name}%"];
        }

        if ($key){
            $where[] = ['key', 'like', "%{$key}%"];
        }
        $lists = new Model();
        if (!empty($where)){
                //dd($where);
            $lists = $lists->where($where)->orderBy('game_id','DESC')->paginate($pageSize);
            //dd($lists);
        } else{
            $lists = $lists::orderBy('game_id','DESC')->paginate($pageSize);
        }


        return view('nonPublicVersion.gameConfig.index',[
            'lists' => $lists,
            'pageSize' => $pageSize,
            'params' => http_build_query($query),
            'query' => $queryParams
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('nonPublicVersion.gameConfig.add');
    }

    public function getGames(Request $request)
    {
        $keywords = $request->get('keywords','');
        $games = $oldData = Model::where('app_name','like', "%{$keywords}%")->get();
        foreach ($games as $key => $game) {
            $games[$key]['label'] = $game['app_name'];
            $games[$key]['value'] = $game['game_id'];
            unset($games[$key]['game_id']);
            unset($games[$key]['app_name']);
        }
        return $this->getView($games,SuccessCode::GET_SUCCESS);
    }

    public function getChannel(Request $request)
    {
        $channels = Channel::where('pid','!=',0)->get();
        $games = [];
        foreach ($channels as $key => $channel) {
            $games[$key]['label'] = $channel->name;
            $games[$key]['value'] = $channel->id;
        }
        return $this->getView($games,SuccessCode::GET_SUCCESS);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(GameConfigRequest $request)
    {
        $data = $request->all();
        $uuid = Uuid::uuid1()->toString();
        $data['value'] = $uuid;

        /*$userInfo = Auth::user();
        $adminId = $userInfo->id;*/

        Model::create($data);

        return $this->getView([],SuccessCode::ADD_SUCCESS);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $oldData = Model::where('game_id',$id)->first();
        return view('nonPublicVersion.gameConfig.edit',[
            'oldData' => $oldData
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(GameConfigRequest $request, $id)
    {
        $data = $request->all();
        $res = Model::find($id);
        //如果此游戏没有KEY。要把KEY加上
        if (!$res->key){
            $uuid = Uuid::uuid1()->toString();
            $data['value'] = $uuid;
        }
        $result = Model::find($id)->update($data);
        if ($result){
            return $this->getView([],SuccessCode::EDIT_SUCCESS);
        } else {
            return $this->getView([],ErrorCode::EDIT_ERROR);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $ids = request()->get('ids','');
        if ($ids){
            $ids = explode(',',$ids);
            foreach ($ids as $id) {
                if (Model::find($id)){
                    $result = Model::find($id)->delete();
                }
            }
            return $this->getView([],SuccessCode::DEL_SUCCESS);
        } else {
            if (Model::find($id)){
                $result = Model::find($id)->delete();
                if ($result){
                    return $this->getView([],SuccessCode::DEL_SUCCESS);
                } else {
                    return $this->getView([],ErrorCode::DEL_ERROR);
                }
            }
        }
        return $this->getView([],ErrorCode::GAME_CONF_EXIST);
    }

    /**
     * @return string
     */
    public function checkGame(Request $request)
    {
        $name = $request->input('name','');
        $queryRes = $this->remoteQuery('appLib/sdk_search_app_name',[
            'keywords' => $name,
        ]);
        if (!empty($queryRes)){
            foreach ($queryRes as $queryRe) {
                if ($queryRe['app_name'] == $name){
                    return $this->getView([],ErrorCode::GAME_EXIST);
                }
            }
        }
        return $this->getView([],SuccessCode::GET_SUCCESS);
    }
}
